package com.sinodata.bsm.center.web.action.performance;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sinodata.bsm.center.cache.RealTimeViewConfCache;
import com.sinodata.bsm.center.expression.BatchCalctor;
import com.sinodata.bsm.center.service.performance.RealTimeService;
import com.sinodata.bsm.common.vo.RealTimeViewConf;
import com.sinodata.bsm.common.vo.TopoTooltipConf;

@Component("performanceImpl")
public class PerformanceImpl implements IPerformance {

    @Autowired
    RealTimeService realTimeService;

    @Autowired
    RealTimeViewConfCache realTimeViewConfCache;

    @Override
    public String queryConf(String resTypeId) {
        RealTimeViewConf conf = realTimeService.findByResTypeId(Long.parseLong(resTypeId));
        if (conf != null && conf.getXml() != null) {
            return conf.getXml();
        } else {
            return "false";
        }
    }

    @Override
    public Long saveConf(String resTypeId, String xml) {
        RealTimeViewConf conf = realTimeService.findByResTypeId(Long.parseLong(resTypeId));
        if (conf == null) {
            conf = new RealTimeViewConf();
            conf.setResTypeId(Long.parseLong(resTypeId));
        }
        conf.setXml(xml);
        conf.setModifyTime(new Date());
        realTimeService.saveRealTimeViewConf(conf);
        return conf.getId();
    }

    public Long saveConfById(String id, String xml) {
        RealTimeViewConf conf = realTimeViewConfCache.get(Long.parseLong(id));
        if (conf != null) {
            conf.setXml(xml);
            realTimeService.saveRealTimeViewConf(conf);
            return conf.getId();
        }
        return -1L;
    }

    @Override
    public String queryRealData(String inStr) {
        BatchCalctor bc = new BatchCalctor();
        String ret = bc.calcXml(inStr);
        return ret;
    }

    @Override
    public String queryTooltipConf(String resTypeId) {
        TopoTooltipConf conf = realTimeService.findTooltipByResTypeId(Long.parseLong(resTypeId));
        if (conf != null && conf.getXml() != null) {
            return conf.getXml();
        } else {
            return "false";
        }
    }

    @Override
    public Long saveTooltipConf(String resTypeId, String xml) {
        TopoTooltipConf conf = realTimeService.findTooltipByResTypeId(Long.parseLong(resTypeId));
        if (conf == null) {
            conf = new TopoTooltipConf();
            conf.setResTypeId(Long.parseLong(resTypeId));
        }
        conf.setXml(xml);
        conf.setModifyTime(new Date());
        realTimeService.saveTooltipConf(conf);
        return conf.getId();
    }

    @Override
    public String queryConfById(String id) {
        RealTimeViewConf conf = realTimeViewConfCache.get(Long.parseLong(id));
        if (conf != null) {
            return conf.getXml();
        }
        return null;
    }

    @Override
    public Long delConf(String id) {
        RealTimeViewConf conf = realTimeService.delRealTimeViewConf(Long.parseLong(id));
        if (conf != null) {
            return conf.getId();
        }
        return null;
    }

}
